package net.orange.framework.logic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import net.orange.architecture.db.Condition;
import net.orange.architecture.db.Conditions;
import net.orange.architecture.db.Operator;
import net.orange.architecture.db.Order;
import net.orange.architecture.db.Orders;
import net.orange.architecture.process.BaseProcess;
import net.orange.framework.model.Group;
import net.orange.framework.model.User;

/**
 * The menu business logical process object which implement the IMenuProcess
 * interface.
 */
public class MenuProcess extends BaseProcess implements IMenuProcess {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.orange.framework.menu.process.IMenuProcess#get(net.orange.framework
	 * .user.User)
	 */
	public Collection get(User user) throws Exception {
		// return the user or his group is empty.
		if (user == null || user.getGroups() == null
				|| user.getGroups().size() == 0)
			return null;

		Collection<Long> pks = new ArrayList<Long>();

		for (Iterator groups = user.getGroups().iterator(); groups.hasNext();) {
			Group group = (Group) groups.next();
			pks.add(new Long(group.getPk()));
		}
		Conditions cnds = new Conditions();
		cnds.add(new Condition("groups.pk", new Operator(Operator.IN_NUM), pks));

		Orders ords = new Orders();

		ords.add(new Order("level", Order.ASC));
		ords.add(new Order("parent", Order.ASC));
		ords.add(new Order("seq", Order.ASC));

		return getDao().list(cnds, ords);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.orange.framework.logic.IMenuProcess#find(net.orange.framework.model
	 * .Group)
	 */
	public Collection find(Group group) throws Exception {
		if (group == null)
			return null;

		Conditions cnds = new Conditions();
		cnds.add(new Condition("groups.pk", group.getPk()));

		return dao.list(cnds);
	}
}
